﻿using System;
using System.IO;
using DevExpress.XtraTreeList.Nodes;
using System.Windows.Forms;
using BranchOfSansar.Sansar.Enquiry;

namespace BranchOfSansar.Register.Main
{
    public partial class frmCustomerList : Form
    {
        private bool IsBrowse;
        public string retVal;
        private bool tlstLoaded ;
        public vw_Customers selectedItem;
        public frmCustomerList()
        {            
            IsBrowse = false;
            InitializeComponent();
        }
        public frmCustomerList(bool is_browse)
            : this()
        {
            this.IsBrowse = is_browse;
        }
        void test(ref string val)
        { 
            //val
        }
        private void frmCustomerList_Load(object sender, EventArgs e)
        {
            if (File.Exists(this.Name + ".xml"))
            {
                viewCust.SaveLayoutToXml(this.Name + ".xml");
            }
            clearSearch();
            loadData();
            tlstLoaded = true;
        }      

        

        private void loadData()
        {
            using (EnquiryClient obj = new EnquiryClient(Globals.Token))
            {
                #region set Treelist data
                var Dist = obj.DistrictAndQuarterList();

                treeList1.BeginUpdate();
                treeList1.Columns.Add();
                treeList1.Columns[0].Caption = "Дүүрэг, хороо";
                treeList1.Columns[0].VisibleIndex = 0;
                treeList1.EndUpdate();
                treeList1.BeginUnboundLoad();
                TreeListNode parentNode = treeList1.AppendNode(new object[] { "Бүгд" }, null);
                parentNode.Tag = "";
                string distCode = "Бүгд";
                foreach (vw_DistAndQuart d in Dist)
                {
                    if (distCode.Equals(d.DistrictCode))
                    {
                        TreeListNode childNode = treeList1.AppendNode(new object[] { d.QuartName }, parentNode);
                        childNode.Tag = d.QuarterCode;
                    }
                    else
                    {
                        parentNode = treeList1.AppendNode(new object[] { d.DistName }, null);
                        parentNode.Tag = d.DistrictCode;
                        TreeListNode childNode = treeList1.AppendNode(new object[] { d.QuartName }, parentNode);
                        childNode.Tag = d.QuarterCode;
                        distCode = d.DistrictCode;
                    }
                }
                treeList1.EndUnboundLoad();
                #endregion
            }

        }
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            clearSearch();
        }
        private void clearSearch()
        {
            cmbStatus.SelectedIndex = 0;
            cmbType.SelectedIndex=0;
            txtDugaar.Text = string.Empty;
            txtRegister.Text = string.Empty;
            txtName.Text = string.Empty;
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            loadGrid();
        }
        private void loadGrid()
        {
            string Dist = string.Empty;
            string Quart = string.Empty;
            if (treeList1.Selection[0].ParentNode == null)
            {
                Dist = treeList1.Selection[0].Tag.ToString();
            }
            else
            {
                Quart = treeList1.Selection[0].Tag.ToString();
            }
            using (EnquiryClient obj = new EnquiryClient(Globals.Token))
            {
                gridControl1.DataSource = obj.CustomerList(Dist,Quart, cmbType.SelectedIndex, cmbStatus.SelectedIndex, txtRegister.Text, txtDugaar.Text, txtName.Text,txtPhone.Text);
            }
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            frmCustomer frm = new frmCustomer();
            frm.Tag = "new";
            if (frm.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                loadGrid();
            }
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (viewCust.SelectedRowsCount < 1)
            {
                Globals.Warr("Өгөгдөл сонгогдоогүй байна!");
                return;
            }

            frmCustomer frm = new frmCustomer();
            vw_Customers cust = (vw_Customers)viewCust.GetRow(viewCust.GetSelectedRows()[0]);
            frm.Tag = cust.CustNo;
            if (frm.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                loadGrid();
            }
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {            
            if (viewCust.SelectedRowsCount < 1)
            {
                Globals.Warr("Өгөгдөл сонгогдоогүй байна!");
                return;
            }
            
            if (IsBrowse)
            {
                selectedItem = (vw_Customers)viewCust.GetRow(viewCust.GetSelectedRows()[0]);                
                this.DialogResult = System.Windows.Forms.DialogResult.Yes;
            }
            else
            {
                barButtonItem2_ItemClick(null, null);
            }
        }

        private void barButtonItem7_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            
        }

        private void barButtonItem8_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                saveFileDialog1.Title = "Specify Destination Filename";
                saveFileDialog1.Filter = "excell Files|*.xls";
                saveFileDialog1.FilterIndex = 1;
                saveFileDialog1.OverwritePrompt = true;
                saveFileDialog1.CreatePrompt = true;

                if (saveFileDialog1.ShowDialog() == DialogResult.OK & !string.IsNullOrEmpty(saveFileDialog1.FileName))
                {

                    gridControl1.ExportToExcelOld(saveFileDialog1.FileName);
                }
            }
            catch (Exception ex)
            {
                Globals.Err(ex.Message);
            }
        }

        private void frmCustomerList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F3)
            {
                loadGrid();
            }
        }

        private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            if (tlstLoaded == false) return;
            if (treeList1.Selection[0].ParentNode == null)
            {
                //if (treeList1.Selection[0].Tag.ToString().Length > 0)
                //{
                //   loadGrid();
                //}

            }
            else
            {
                loadGrid();
            }
        }

        private void gridControl1_Click(object sender, EventArgs e)
        {

        }

        private void frmCustomerList_FormClosing(object sender, FormClosingEventArgs e)
        {
            viewCust.SaveLayoutToXml(this.Name + ".xml");
        }

    }
}
